#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# :CREATED: [2018-04-26 Thu 21:37]
# :AUTHOR: zb
from flask import (
    render_template,
    request,
    redirect,
    url_for,
    Blueprint,
    abort,
    Response,
    flash,
)

from models.server import (
    Server,
    ServerForm,
    ServerTime,
    ServerQueryForm,
)
from models.base import db
from routes.server.time_check import servers_check_time
from routes.server.sync_server import ntp_sync_all
from routes.server.tool import ZONE
import time
from untils.untils import log


main = Blueprint('server', __name__)


@main.route("/")
def index():
    form = request.args
    domains = [d.domain for d in db.query(Server.domain).distinct()]
    if form.get('domain'):
        servers = Server.find_by(domain=form.get('domain'))
    else:
        servers = Server.find_all()
    return render_template("server/index.html", servers=servers, domains=domains)


@main.route("/add", methods=['GET', 'POST'])
def add():

    form = ServerForm()
    if form.validate_on_submit():
        add_key = [
            'ip',
            'username',
            'password',
            'domain',
            'servername',
        ]
        _form = {}
        for k in add_key:
            _form[k] = form.data.get(k)
        n_server = Server.new(**_form)
        Server.insert(n_server)
        Server.commit()
        return redirect(url_for('.index'))
    return render_template('server/add.html', form=form)


@main.route("/delete")
def delete():

    servers = Server.find_all()

    return render_template("server/index.html", servers=servers)


@main.route("/update")
def update():

    servers = Server.find_all()

    return render_template("server/index.html", servers=servers)


@main.route("/check_time")
def check_time():
    st = time.time()
    domain = request.args.get('domain')
    domains = [d.domain for d in db.query(Server.domain).distinct()]

    s = db.query(Server.id, 
                Server.ip, 
                Server.domain, 
                Server.servername,
                ServerTime.server_time, 
                ServerTime.update_time)
    if domain:  
        servers = s.filter(Server.id == ServerTime.server_id,
                    Server.domain==domain).all()
    else:
        servers = s.filter(Server.id == ServerTime.server_id).all()
    et = time.time()
    cost_time = et - st
    return render_template("server/check_time.html",
                    servers=servers,
                    cost_time=cost_time,domains=domains)


@main.route("/check_time/real_time")
def check_on_real_time():
    domain = request.args.get('domain')
    if domain:
        servers = Server.find_by(domain=domain)
    else:
        servers = Server.find_all()
    servers_check_time(servers)
    return redirect(url_for('.check_time'))

@main.route("/check_time/sync", methods=['GET', 'POST'])
def sync_servertime():
    if request.method == 'GET':
        servers_id = request.args.get('server_id', '-1').split(',')
        print('--'*10,servers_id) 
        timeZone = ZONE.keys()
        return render_template('server/sync_server.html', 
            servers_id=servers_id,time_zone=timeZone)
    else:
        servers_id = request.form.get('server_id').split(',')
        timeZone = request.form.get('time_zone')
        ntp_server = '10.119.67.61'

        servers = db.query(Server).filter(Server.id.in_(servers_id))
        ntp_sync_all(servers, ntp_server, timeZone)
        flash('sync_servertime running!')
        return redirect(url_for('.check_time'))

if __name__ == '__main__':
    pass
